package com.zmn.oms.business.impl.conf.attachment;

import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.oms.business.interfaces.conf.attachment.ConfOrderAttachmentCategoryBService;
import com.zmn.oms.common.constant.OrderAttachmentCategoryTypeEnum;
import com.zmn.oms.common.exception.OmsBaseException;
import com.zmn.oms.model.entity.conf.attachment.ConfOrderAttachmentCategory;
import com.zmn.oms.model.entity.conf.attachment.ConfOrderAttachmentCategoryQuery;
import com.zmn.oms.model.vo.conf.attachment.ConfOrderAttachmentCategoryVO;
import com.zmn.oms.services.interfaces.conf.attachment.ConfOrderAttachmentCategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

@Service
@Slf4j
public class ConfOrderAttachmentCategoryBServiceImpl implements ConfOrderAttachmentCategoryBService {

    @Autowired
    private ConfOrderAttachmentCategoryService confOrderAttachmentCategoryService;

    @Override
    public List<ConfOrderAttachmentCategoryVO> listPageByQuery(ConfOrderAttachmentCategoryQuery query) {
        if (StringUtils.isNotBlank(query.getKeyWord())) {
            if (NumberUtils.isDigits(query.getKeyWord())) {
                query.setId(Integer.valueOf(query.getKeyWord()));
            } else {
                query.setLikeName(query.getKeyWord());
            }
        }
        List<ConfOrderAttachmentCategory> confList = confOrderAttachmentCategoryService.listPageByQuery(query);
        List<ConfOrderAttachmentCategoryVO> confOrderAttachmentCategoryVOList = BeanMapper.mapList(confList, ConfOrderAttachmentCategoryVO.class);
        confOrderAttachmentCategoryVOList.forEach(conf->conf.setTypeName(OrderAttachmentCategoryTypeEnum.cache.get(conf.getType()).getName()));
        return confOrderAttachmentCategoryVOList;
    }


    @Override
    public void modifyCategoryById(ConfOrderAttachmentCategory category) throws OmsBaseException {
        Objects.requireNonNull(category.getCategId(), "ID不能为空");
        ConfOrderAttachmentCategory updateCategory = ConfOrderAttachmentCategory.builder()
                .categId(category.getCategId())
                .type(category.getType())
                .status(category.getStatus())
                .sort(category.getSort())
                .categName(category.getCategName())
                .remark(category.getRemark())
                .updater(category.getUpdater()).build();
        confOrderAttachmentCategoryService.updateCategoryById(updateCategory);
    }

    @Override
    public void modifyCategoryStatusById(ConfOrderAttachmentCategory category) throws OmsBaseException {
        Objects.requireNonNull(category.getCategId(), "ID不能为空");
        ConfOrderAttachmentCategory updateCategory = ConfOrderAttachmentCategory.builder()
                .categId(category.getCategId())
                .status(category.getStatus())
                .updater(category.getUpdater()).build();
        confOrderAttachmentCategoryService.updateCategoryById(updateCategory);
    }

    @Override
    public void modifyCategorySortById(ConfOrderAttachmentCategory category) {
        Objects.requireNonNull(category.getCategId(), "ID不能为空");

        ConfOrderAttachmentCategory updateCategory = ConfOrderAttachmentCategory.builder()
                .categId(category.getCategId())
                .sort(category.getSort())
                .updater(category.getUpdater()).build();
        confOrderAttachmentCategoryService.updateCategoryById(updateCategory);
    }

    @Override
    @Transactional
    public Integer addCategory(ConfOrderAttachmentCategory category) throws OmsBaseException {
        return confOrderAttachmentCategoryService.insertCategory(category);
    }

    @Override
    public Integer batchModifyCategoryStatus(ConfOrderAttachmentCategory conf, List<String> categIdList) {
        return confOrderAttachmentCategoryService.batchModifyCategoryStatus(conf,categIdList);
    }
}
